<?php
/*
 * class action for update user password(reset password) in profile view
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

class UpdatePasswordAction extends CAction {

   public function run() {

      if (isset($_GET['id'])) {
         $controller = $this->getController();
         $model_fogotpsw = fogetPassword::model()->findByAttributes(array('tokenid' => $_GET['id']));
         if (!is_null($model_fogotpsw)) {
            $model = User::model()->loadModel($model_fogotpsw->user_id);
         } else {
            $model = new User();
            Yii::app()->user->setFlash('error', "Your Token Expired!!  try again >>");
         }


         if (isset($_POST['User'])) {
            $model->password = User::model()->encryptPassword($_POST['User']['new_password']);

            if (!$model->save()) {
               print_r($model->getErrors());
               exit;
            } else {
               Yii::app()->user->setFlash('success', "Your Password Reset successfully!!  Go to Login >>");
            }
         }

         $controller->render('resetPassword', array(
             'model' => $model,
         ));
      }
   }
}
